上一篇有基本指令的介紹,接下來看似重複的操作,有助於進一步瞭解庫與表的關系以及除錯的錯行。
MariaDB [(none)]> create database brad;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| brad |
| class |
| cy |
| db1 |
| iii |
| information_schema |
| mysql |
| northwind |
| performance_schema |
| phpmyadmin |
| test |
+--------------------+
MariaDB [(none)]> use brad;
Database changed
MariaDB [brad]> select database();
+------------+
| database() |
+------------+
| brad |
+------------+
MariaDB [brad]> select database();
+------------+
| database() |
+------------+
| brad |
+------------+
MariaDB [brad]> create table cust(id int, cname varchar(100), tel varchar(20) , birthday date);
Query OK, 0 rows affected (0.107 sec)
MariaDB [brad]> show tables;
+----------------+
| Tables_in_brad |
+----------------+
| cust |
+----------------+
1 row in set (0.001 sec)
MariaDB [brad]> desc cust;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| cname | varchar(100) | YES | | NULL | |
| tel | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
MariaDB [brad]> select * from cust;
Empty set (0.000 sec)
MariaDB [brad]> insert into cust value (1, 'brad' ,'123' , '1999-01-02');
Query OK, 1 row affected (0.050 sec)
MariaDB [brad]> select * from cust;
+------+-------+------+------------+
| id | cname | tel | birthday |
+------+-------+------+------------+
| 1 | brad | 123 | 1999-01-02 |
+------+-------+------+------------+
1 row in set (0.000 sec)
MariaDB [brad]> insert into cust value (3, 'john' , '456','2122-09-09');
Query OK, 1 row affected (0.050 sec)
MariaDB [brad]> select * from cust;
+------+-------+------+------------+
| id | cname | tel | birthday |
+------+-------+------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
+------+-------+------+------------+
3 rows in set (0.000 sec)
MariaDB [brad]> insert into cust value ('cy', 'john' , '456','2122-09-09');
Query OK, 1 row affected, 1 warning (0.050 sec)
MariaDB [brad]> select * from cust;
+------+-------+------+------------+
| id | cname | tel | birthday |
+------+-------+------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
+------+-------+------+------------+
MariaDB [brad]> insert into cust value (9487945123, 'john' , '456','2122-09-09');
Query OK, 1 row affected, 1 warning (0.050 sec)
//因為id輸入的數值已經超過欄位最高數值21億....故只顯示最高數值21億....
MariaDB [brad]> select * from cust;
+------------+-------+------+------------+
| id | cname | tel | birthday |
+------------+-------+------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
+------------+-------+------+------------+
5 rows in set (0.000 sec)
MariaDB [brad]> insert into cust(cname, tel ,birthday) value ('tony' ,'666', '2003-09-08');
Query OK, 1 row affected (0.051 sec)
MariaDB [brad]> show tables;
+----------------+
| Tables_in_brad |
+----------------+
| cust |
+----------------+
MariaDB [brad]> select * from cust;
+------------+-------+------+------------+
| id | cname | tel | birthday |
+------------+-------+------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
+------------+-------+------+------------+
MariaDB [brad]> desc cust;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| cname | varchar(100) | YES | | NULL | |
| tel | varchar(20) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
MariaDB [brad]> insert into cust(cname, tel ,birthday) value ('tony' ,'122334444444', '2003-09-08');
Query OK, 1 row affected (0.049 sec)
MariaDB [brad]> show tables;
+----------------+
| Tables_in_brad |
+----------------+
| cust |
+----------------+
MariaDB [brad]> select * from cust;
+------------+-------+--------------+------------+
| id | cname | tel | birthday |
+------------+-------+--------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
+------------+-------+--------------+------------+
MariaDB [brad]> insert into cust(cname, tel ,birthday) value ('tony' ,'12345678901234567890112', '2003-09-08');
Query OK, 1 row affected, 1 warning (0.050 sec)
MariaDB [brad]> select * from cust;
+------------+-------+----------------------+------------+
| id | cname | tel | birthday |
+------------+-------+----------------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
| NULL | tony | 12345678901234567890 | 2003-09-08 |
+------------+-------+----------------------+------------+
8 rows in set (0.000 sec)
MariaDB [brad]> insert into cust(cname, tel ,birthday) value ('tony' ,
-> '今天天氣很好,全班都濕透了,因為沒有冷氣的關係'
-> ,'1999-01-01');
Query OK, 1 row affected, 1 warning (0.051 sec)
MariaDB [brad]> select * from cust;
+------------+-------+-------------------------------------------+------------+
| id | cname | tel | birthday |
+------------+-------+-------------------------------------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
| NULL | tony | 12345678901234567890 | 2003-09-08 |
| NULL | tony | 今天天氣很好,全班都濕透了,因為沒有冷氣 | 1999-01-01 |
+------------+-------+-------------------------------------------+------------+
9 rows in set (0.000 sec)
MariaDB [brad]> insert into cust (birthday) value ('1999-00-34');
Query OK, 1 row affected, 1 warning (0.050 sec)
MariaDB [brad]> select * from cust ;
+------------+-------+----------------------+------------+
| id | cname | tel | birthday |
+------------+-------+----------------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
| NULL | tony | 12345678901234567890 | 2003-09-08 |
| NULL | tony | ???????????????????? | 1999-01-01 |
| NULL | NULL | NULL | 0000-00-00 |
+------------+-------+----------------------+------------+
10 rows in set (0.001 sec)
MariaDB [brad]> insert into cust (birthday) value ('1999-01-31 10:10:10');
Query OK, 1 row affected, 1 warning (0.050 sec)
MariaDB [brad]> select *from cust;
+------------+-------+----------------------+------------+
| id | cname | tel | birthday |
+------------+-------+----------------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
| NULL | tony | 12345678901234567890 | 2003-09-08 |
| NULL | tony | ???????????????????? | 1999-01-01 |
| NULL | NULL | NULL | 0000-00-00 |
| NULL | NULL | NULL | 1999-01-31 |
+------------+-------+----------------------+------------+
11 rows in set (0.000 sec)
MariaDB [brad]> insert into cust (birthday) value ('1999-01-31 10:10:80');
Query OK, 1 row affected, 1 warning (0.050 sec)
MariaDB [brad]> select * from cust;
+------------+-------+----------------------+------------+
| id | cname | tel | birthday |
+------------+-------+----------------------+------------+
| 1 | brad | 123 | 1999-01-02 |
| 2 | tony | 321 | 2001-09-09 |
| 3 | john | 456 | 2122-09-09 |
| 0 | john | 456 | 2122-09-09 |
| 2147483647 | john | 456 | 2122-09-09 |
| NULL | tony | 666 | 2003-09-08 |
| NULL | tony | 122334444444 | 2003-09-08 |
| NULL | tony | 12345678901234567890 | 2003-09-08 |
| NULL | tony | ???????????????????? | 1999-01-01 |
| NULL | NULL | NULL | 0000-00-00 |
| NULL | NULL | NULL | 1999-01-31 |
| NULL | NULL | NULL | 0000-00-00 |
+------------+-------+----------------------+------------+
12 rows in set (0.000 sec)
MariaDB [iii]> insert into tb1 (id, f1) values (5,'APPLE');
Query OK, 1 row affected (0.050 sec)
MariaDB [iii]> select * from tb1;
+-----+-------+
| id | f1 |
+-----+-------+
| 1 | CY |
| 2 | Brad |
| 5 | APPLE |
| 99 | 1111 |
| 100 | qqq |
+-----+-------+
MariaDB [iii]> insert into tb1 (f1) values ('qqq');
//沒給指定數字會遞增加入
Query OK, 1 row affected (0.049 sec)
MariaDB [iii]> select * from tb1;
+-----+-------+
| id | f1 |
+-----+-------+
| 1 | CY |
| 2 | Brad |
| 5 | APPLE |
| 99 | 1111 |
| 100 | qqq |
| 101 | qqq |
+-----+-------+
MariaDB [iii]> select * from tb1;
+-----+-------+
| id | f1 |
+-----+-------+
| 1 | CY |
| 2 | Brad |
| 5 | APPLE |
| 99 | 1111 |
| 100 | qq11 |
| 101 | qq11 |
| 102 | apple |
+-----+-------+
MariaDB [iii]> alter table tb1 modify f1 varchar(2);//重設長度為2
Query OK, 7 rows affected, 1 warning (0.190 sec)
Records: 7 Duplicates: 0 Warnings: 1
MariaDB [iii]> select * from tb1;
//原本超過的長度字元被截斷了
+-----+------+
| id | f1 |
+-----+------+
| 1 | CY |
| 2 | Br |
| 5 | AP |
| 99 | 11 |
| 100 | qq |
| 101 | qq |
| 102 | ap |
+-----+------+
MariaDB [iii]> insert into tb1 (f1) value (apple);
ERROR 1054 (42S22): Unknown column 'apple' in 'field list'
MariaDB [iii]> alter table tb1 modify f1 int;
Query OK, 10 rows affected, 6 warnings (0.120 sec)
Records: 10 Duplicates: 0 Warnings: 6
MariaDB [iii]> select * from tb1;
//原本字串部分都變成0
+-----+------+
| id | f1 |
+-----+------+
| 1 | 0 |
| 2 | 0 |
| 5 | 0 |
| 99 | 11 |
| 100 | 0 |
| 101 | 0 |
| 102 | 0 |
| 103 | 11 |
| 104 | 12 |
| 105 | -1 |
+-----+------+
MariaDB [iii]> alter table tb1 add f2 varchar(12);
Query OK, 0 rows affected (0.019 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [iii]> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| f1 | int(11) | YES | | NULL | |
| f2 | varchar(12) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
MariaDB [iii]> alter table tb1 add f3 varchar(10) after f1;
Query OK, 0 rows affected (0.109 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [iii]> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| f1 | int(11) | YES | | NULL | |
| f3 | varchar(10) | YES | | NULL | |
| f2 | varchar(12) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
MariaDB [iii]> alter table tb1 add f4 varchar(10) first;
//最後first 則會顯示在第一個欄位
Query OK, 0 rows affected (0.062 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [iii]> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| f4 | varchar(10) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| f1 | int(11) | YES | | NULL | |
| f3 | varchar(10) | YES | | NULL | |
| f2 | varchar(12) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
MariaDB [iii]> alter table tb1 change f1 newf1 varchar(11);
Query OK, 10 rows affected (0.069 sec)
Records: 10 Duplicates: 0 Warnings: 0
MariaDB [iii]> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| f4 | varchar(10) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| newf1 | varchar(11) | YES | | NULL | |
| f3 | varchar(10) | YES | | NULL | |
| f2 | varchar(12) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
MariaDB [iii]> alter table tb1 drop f2;//將f2欄位刪除
Query OK, 0 rows affected (0.107 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [iii]> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| f4 | varchar(10) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| newf1 | varchar(11) | YES | | NULL | |
| f3 | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
MariaDB [iii]> update tb1 set f4 ='brad';
Query OK, 10 rows affected (0.002 sec)
Rows matched: 10 Changed: 10 Warnings: 0
MariaDB [iii]> select *from tb1;//因為沒有設條件 所以都會顯示brad
+------+-----+-------+------+
| f4 | id | newf1 | f3 |
+------+-----+-------+------+
| brad | 1 | 0 | NULL |
| brad | 2 | 0 | NULL |
| brad | 5 | 0 | NULL |
| brad | 99 | 11 | NULL |
| brad | 100 | 0 | NULL |
| brad | 101 | 0 | NULL |
| brad | 102 | 0 | NULL |
| brad | 103 | 11 | NULL |
| brad | 104 | 12 | NULL |
| brad | 105 | -1 | NULL |
+------+-----+-------+------+
MariaDB [iii]> update tb1 set f4 ='Eric' where id =103;
Query OK, 1 row affected (0.012 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [iii]> select *from tb1;
+------+-----+-------+------+
| f4 | id | newf1 | f3 |
+------+-----+-------+------+
| brad | 1 | 0 | NULL |
| brad | 2 | 0 | NULL |
| brad | 5 | 0 | NULL |
| brad | 99 | 11 | NULL |
| brad | 100 | 0 | NULL |
| brad | 101 | 0 | NULL |
| brad | 102 | 0 | NULL |
| Eric | 103 | 11 | NULL |
| brad | 104 | 12 | NULL |
| brad | 105 | -1 | NULL |
+------+-----+-------+------+
MariaDB [iii]> update tb1 set f4 ='brad' ,f3='test' where id>=100;
Query OK, 6 rows affected (0.013 sec)
Rows matched: 6 Changed: 6 Warnings: 0
MariaDB [iii]> select *from tb1;
+------+-----+-------+------+
| f4 | id | newf1 | f3 |
+------+-----+-------+------+
| brad | 1 | 0 | NULL |
| brad | 2 | 0 | NULL |
| brad | 5 | 0 | NULL |
| brad | 99 | 11 | NULL |
| brad | 100 | 0 | test |
| brad | 101 | 0 | test |
| brad | 102 | 0 | test |
| brad | 103 | 11 | test |
| brad | 104 | 12 | test |
| brad | 105 | -1 | test |
+------+-----+-------+------+
MariaDB [iii]> delete from tb1 where id = 103;
Query OK, 1 row affected (0.014 sec)
MariaDB [iii]> select *from tb1;
+------+-----+-------+------+
| f4 | id | newf1 | f3 |
+------+-----+-------+------+
| brad | 1 | 0 | NULL |
| brad | 2 | 0 | NULL |
| brad | 5 | 0 | NULL |
| brad | 99 | 11 | NULL |
| brad | 100 | 0 | test |
| brad | 101 | 0 | test |
| brad | 102 | 0 | test |
| brad | 104 | 12 | test |
| brad | 105 | -1 | test |
+------+-----+-------+------+
So far so good! 庫與表的遠征之路還沒正式開始呢!
to be continued~